package com.icetech.eom.service.impl.tx;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.icetech.eom.common.constant.UserTypeEnum;
import com.icetech.eom.dao.sys.CustomDao;
import com.icetech.eom.dao.sys.PermissionDao;
import com.icetech.eom.dao.sys.UserRoleDao;
import com.icetech.eom.dao.tx.UserDao;
import com.icetech.eom.domain.sys.*;
import com.icetech.eom.domain.tx.dto.UserDto;
import com.icetech.eom.domain.tx.dto.UserExcelDto;
import com.icetech.eom.service.sys.IUserRoleService;
import com.icetech.eom.service.tx.IUserService;
import com.icetech.eom.service.utils.SecurityUtil;
import com.xuxueli.poi.excel.ExcelExportUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangzw
 * @since 2018-11-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private SecurityUtil securityUtil;
    @Autowired
    private CustomDao customDao;
    @Override
    public PageInfo<UserDto> selectByPage(String searchKey, Integer page, Integer pageSize,Integer userType,Long orgId) {
        PageHelper.startPage(page,pageSize);
        List<UserDto> userDtos = userDao.selectBySearchKey(searchKey,userType,orgId);
        //获取用户的角色信息
        userDtos.stream().forEach(user->{
            List<Role> roles = userRoleDao.findByUserId(user.getId());
            user.setRoles(roles);
            user.setPwd("");
            //获取客户信息
            if (user.getOrgId()!= null){
                Custom custom = customDao.selectById(user.getOrgId());
                if (Objects.nonNull(custom)) user.setCompName(custom.getCompanyName());
            }
        });
        return new PageInfo(userDtos);
    }

    @Override
    public Workbook exportExcel(String searchKey,Integer userType,Long orgId) {
        List<UserDto> userDtos = userDao.selectBySearchKey(searchKey,userType,orgId);
        List<UserExcelDto> userExcelDtos = Lists.newArrayList();
        userDtos.forEach(userDto -> {
            UserExcelDto userExcelDto = new UserExcelDto();
            BeanUtils.copyProperties(userDto, userExcelDto);
            if (userDto.getIsEnabled()){
                userExcelDto.setIsEnabledStr("可用");
            }else {
                userExcelDto.setIsEnabledStr("不可用");
            }
            //获取客户信息
            if (userDto.getOrgId()!= null){
                Custom custom = customDao.selectById(userDto.getOrgId());
                if (Objects.nonNull(custom)) userExcelDto.setCompName(custom.getCompanyName());
            }
            //设置账户类型
            userExcelDto.setUserType(UserTypeEnum.getDescByType(userDto.getUserType()));
            userExcelDtos.add(userExcelDto);
        });
        return ExcelExportUtil.exportWorkbook(userExcelDtos);
    }

    @Override
    public User findByUsername(String username) {
        User user = userDao.selectByUserName(username);
        if (Objects.isNull(user)){
            return null;
        }
        //关联角色信息
        List<Role> roleList = userRoleDao.findByUserId(user.getId());
        user.setRoles(roleList);
        // 关联权限菜单
        List<Permission> permissionList = permissionDao.findByUserId(user.getId());
        user.setPermissions(permissionList);
        return user;
    }

    @Override
    public User findLoginByUsername(String username) {
        User user = userDao.selectByLoginUserByName(username);
        if (Objects.isNull(user)){
            return null;
        }
        //关联角色信息
        List<Role> roleList = userRoleDao.findByUserId(user.getId());
        user.setRoles(roleList);
        // 关联权限菜单
        List<Permission> permissionList = permissionDao.findByUserId(user.getId());
        user.setPermissions(permissionList);
        return user;
    }

    @Override
    public User findClientByUsername(String username) {
        User user = userDao.selectByClientUserByName(username);
        return user;
    }

    @Override
    public boolean saveUserAndRole(User user,List<Long> roleIds) {
        // 保存用户
        this.save(user);
        if (!CollectionUtils.isEmpty(roleIds)){
            // 保存用户角色关联信息
            List<UserRole> userRoles = Lists.newArrayList();
            User currUser = securityUtil.getCurrUser();
            roleIds.forEach(integer -> {
                UserRole userRole = new UserRole();
                userRole.setRoleId(integer);
                userRole.setUserId(user.getId());
                userRole.setCreateBy(currUser.getUserName());
                userRoles.add(userRole);
            });
            userRoleService.saveBatch(userRoles);
        }

        return true;
    }

    @Override
    public User findDeveloperByName(String username) {
        return userDao.findDeveloperByName(username);
    }

    @Override
    public List<User> findAllDeveloper() {
        return userDao.findAllDeveloper();
    }

    @Override
    public List<User> getOperatersByDev(Long devUserId) {
        return userDao.getOperatersByDev(devUserId);
    }
}

